package source.events;

import source.view.MainScreen;
import source.view.ViewData;
import source.view.ViewMap;
import source.view.viewobjects.*;

 
public class ViewEventListener implements EventListener<ViewEvent> {

	private ViewData viewData;
	private MainScreen ms;
	
	public ViewEventListener(ViewData viewData)
	{
		this.viewData = viewData;
	}
	
	public void setMainScreen(MainScreen ms){
		this.ms = ms;
	}
	/**
	 * eventCallback method gets called by the event handler when a ModelEvent is fired
	 *   The switch statement checks the ModelEventType enum against the type attribute in event
	 */
	public boolean eventCallback(ViewEvent e)
	{
		
		switch(ViewEventType.valueOf(e.getType()))
		{
			case NEWTURN:
				viewData.setCurrentPlayer(e.getPlayerID());
				//MainScreen.writeToConsole("ViewEventListener:  Changed Listener to Player " 
				//		+ e.getPlayerID() + "'s");
				return true;
				
			case ADD_VIEW_OBJECT:
				{
					// The event must be a view object event, so this typecast is safe
					ViewObjectEvent ve = (ViewObjectEvent)e;
					ViewObject vobj;
					
					// Depending on what type the game object is, we add it in a differnt way
					switch(ve.getTargetObject().objectType())
					{
					case UNIT:
						
						vobj = new ViewUnit(
								((source.model.Unit)ve.getTargetObject()).getType(),
								ve.getTargetObject().objectID(), 
								ve.getPlayerID(),
								ve.getTargetObject().getViewData());
						
						viewData.add(vobj, ve.getTargetPosition());
						
						break;
					
					case AREAEFFECT:
						vobj = new ViewAreaEffect(((source.model.AreaEffect)ve.getTargetObject()).getType(),
								ve.getTargetObject().objectID());

						viewData.add(vobj, ve.getTargetPosition());

						break;
				
					case TERRAIN:
						vobj = new ViewTerrain(((source.model.Terrain)ve.getTargetObject()).getType());
						
						viewData.addTerrain((ViewTerrain)vobj, ve.getTargetPosition());
						
						break;
						
					case ITEM:
						vobj = new ViewItem(((source.model.Item)ve.getTargetObject()).getItemType(), ve.getTargetObject().objectID());
						
						viewData.add((ViewItem)vobj, ve.getTargetPosition());
						
						break;
						
					case BASE:
						vobj = new ViewStructure(
								((source.model.Base)ve.getTargetObject()).getType(),
								ve.getPlayerID(),
								ve.getTargetObject().objectID(),
								((source.model.Base)ve.getTargetObject()).getViewData());
						viewData.add(vobj, ve.getTargetPosition());
						
						break;
					}
				}
					
				break;
				
			case MOVE_VIEW_OBJECT:
				{
					ViewObjectEvent ve = (ViewObjectEvent)e;
					
					viewData.move(ve.getTargetObject().objectID(), ve.getTargetPosition());
				}
				
				break;
				
			case REMOVE_VIEW_OBJECT:
				{
					ViewObjectEvent ve = (ViewObjectEvent)e;
					
					viewData.remove(ve.getTargetObject().objectID());
				}
				
				break;
				
			case SELECT_VIEW_OBJECT:
				{
					ViewObjectEvent ve = (ViewObjectEvent)e;
					ViewObject vob = viewData.findViewObject(ve.getTargetObject().objectID());
					
					viewData.setSelectedTile(ve.getTargetPosition());
					viewData.setCurrentViewObject((ViewObjectWithStats)vob);
					if(ms != null) ms.updateScreens();
				}
			
				break;
				
			case CLEAR_VIEW:
				viewData.clearMap();
				
				break;
				
			case MODIFY_VIEW_OBJECT:
				{
					ViewObjectEvent ve = (ViewObjectEvent)e;
					ViewObjectWithStats vob = (ViewObjectWithStats)viewData.findViewObject(ve.getTargetObject().objectID());
					if(ve.getTargetObject().isValid())
						vob.setStats(ve.getTargetObject().getViewData());
					
				}
				break;
				
			case ADD_HIGHLIGHTED_POSITION:
				{
					ViewObjectEvent ve = (ViewObjectEvent) e;
					viewData.addHighlightedTile(ve.getTargetPosition());
				}
				break;
				
			case CLEAR_HIGHLIGHTED:
				{
					ViewObjectEvent ve = (ViewObjectEvent) e;
					viewData.clearHighlightedTiles();
				}
				break;
				
			case SET_CURRENT_POSITION:
				{
					ViewObjectEvent ve = (ViewObjectEvent) e;
					viewData.setSelectedTile(ve.getTargetPosition());
				}
				
				break;
			}
		if(ms != null) ms.updateOverviews();
		return false;
	}
}
